package cn.sstech.member.core.util;

import java.io.File;
import java.util.Iterator;
import java.util.Map;

import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class RestHttpTemplate {

	public static String post(String url, String body) {
		return post(url, body, new HttpHeaders(), String.class,null);
	}

	public static String post(String url, String body, HttpHeaders headers) {
		return post(url, body, headers, String.class,null);
	}

	public static String get(String url, Map<String, Object> urlVariables) {
		return get(url, new HttpHeaders(), urlVariables, String.class);
	}

	/**
	 * 下载文件
	 * 
	 * @param url
	 * @param urlVariables
	 * @param headers
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static byte[] getResponseBody(String url, Map<String, Object> urlVariables, HttpHeaders headers) {
		byte[] result = null;
		RestTemplate restTemplate = new RestTemplate();
		HttpEntity<Object> headerHolder = new HttpEntity<Object>(urlVariables, headers);
		ResponseEntity<byte[]> response = null;
		try {
			response = restTemplate.exchange(url, HttpMethod.GET, headerHolder, byte[].class, urlVariables);
			result = response.getBody();
		} catch (Exception e) {
			return null;
		}

		return result;
	}

	public static String get(String url, HttpHeaders headers, Map<String, Object> urlVariables) {
		return get(url, headers, urlVariables, String.class);
	}

	public static <T> T post(String url, String body, HttpHeaders headers, Class<T> clazz,Map<String, Object> urlVariables) {
		RestTemplate restTemplate = new RestTemplate();
		if (null == headers) {
			headers = new HttpHeaders();
		}
		
		if(null != urlVariables){
			
			if(!url.endsWith("?")){
				url = url+"?";
			}
			
		    Iterator<Map.Entry<String, Object>> it = urlVariables.entrySet().iterator();
		    int index = 0;
		    while (it.hasNext()) {
		      Map.Entry<String, Object> entry = it.next();
		      
		      if(index == 0){
		    	  url = url + entry.getKey() + "="+entry.getValue();
		      }
		      else{
		    	  url = url +"&"+ entry.getKey() + "="+entry.getValue();
		      }
		      
		      index++;
		    }
		}
		
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type); // type??
		// headers.setContentType(MediaType.APPLICATION_JSON);
		HttpEntity<String> formEntity = new HttpEntity<String>(body, headers);
		// 第三个参数可以是bean
		T result = null;
		try {
			result = restTemplate.postForObject(url, formEntity, clazz);
		} catch (Exception e) {
			return result;
		}

		return result;
	}

	public static <T> T get(String url, HttpHeaders headers, Map<String, Object> urlVariables, Class<T> clazz) {
		T result = null;
		
		if(null != urlVariables){
			
			if(!url.endsWith("?")){
				url = url+"?";
			}
			
		    Iterator<Map.Entry<String, Object>> it = urlVariables.entrySet().iterator();
		    int index = 0;
		    while (it.hasNext()) {
		      Map.Entry<String, Object> entry = it.next();
		      
		      if(index == 0){
		    	  url = url + entry.getKey() + "="+entry.getValue();
		      }
		      else{
		    	  url = url +"&"+ entry.getKey() + "="+entry.getValue();
		      }
		      
		      index++;
		    }
		}
		
		RestTemplate restTemplate = new RestTemplate();
		HttpEntity<Object> headerHolder = new HttpEntity<Object>(urlVariables, headers);
		ResponseEntity<T> response = null;
		try {
			response = restTemplate.exchange(url, HttpMethod.GET, headerHolder, clazz, urlVariables);
			result = response.getBody();
		} catch (Exception e) {
			return null;
		}

		return result;
	}

	/**
	 * 上传文件
	 * 
	 * @param url
	 * @param headers
	 * @param file
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static String post(String url, HttpHeaders headers, File file) {
		if (null == headers) {
			headers = new HttpHeaders();
		}
		MediaType type = MediaType.parseMediaType("multipart/form-data");
		headers.setContentType(type);

		RestTemplate rest = new RestTemplate();
		FileSystemResource resource = new FileSystemResource(file);
		MultiValueMap<String, Object> param = new LinkedMultiValueMap<String, Object>();
		param.add("file", resource);
		param.add("fileName", file.getName());

		HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(param,
				headers);
		ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class);

		return responseEntity.getBody();
	}

	public static String getRunTaskExecPath() {
		String webapproot = System.getProperty("webapp.root");
		String filepath = "";
		String[] splitpath = webapproot.split("\\\\");
		for (int i = 0; i < splitpath.length - 1; i++) {
			filepath = filepath + splitpath[i] + File.separator;
		}
		filepath = filepath + "whale.service" + File.separator;
		return filepath;
	}

	// public <T> T get(String url,Map<String,Object> urlVariables,Class<T>
	// clazz){
	// RestTemplate restTemplate = new RestTemplate();
	// //第三个参数可以是bean
	// T result = restTemplate.getForObject(url, clazz, urlVariables);
	// return result;
	// }
	
	public static String delete(String url, HttpHeaders headers, Map<String, Object> urlVariables ) {
		String result = null;
		RestTemplate restTemplate = new RestTemplate();
		HttpEntity<Object> headerHolder = new HttpEntity<Object>(urlVariables, headers);
		ResponseEntity<String> response = null;
		try {
			response = restTemplate.exchange(url, HttpMethod.DELETE, headerHolder, String.class, urlVariables);
			result = response.getBody();
		} catch (Exception e) {
			return null;
		}

		return result;
	}

	public static String post(String url,String body, HttpHeaders headers, Map<String, Object> urlVariables) {
		return post(url, body, headers, String.class,urlVariables);
	}
	
}